package com.cupster.base_super_resource;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

public class CommonRowEntranceView  extends RelativeLayout {

    public CommonRowEntranceView(Context context) {
        super(context);
        initInnerView(context);
    }

    public CommonRowEntranceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInnerView(context);
        initAttrs(context, attrs);
    }

    public CommonRowEntranceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInnerView(context);
        initAttrs(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CommonRowEntranceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initInnerView(context);
        initAttrs(context, attrs);
    }

    RelativeLayout mRootLayout ;
    TextView mTitle ;
    ImageView mArrow;

    private void initInnerView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_common_row_entrance, this, true);//此处应为关联true，否则报错
        mRootLayout = findViewById(R.id.layout_root);
        mArrow = findViewById(R.id.icon_arrow);
        mTitle = findViewById(R.id.title);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonRowEntranceView);
        //取值
        Drawable bgDrawable = typedArray.getDrawable(R.styleable.CommonRowEntranceView_row_bg);
        int txtColor= typedArray.getInt(R.styleable.CommonRowEntranceView_row_txt_color, Color.parseColor("#808080"));
        String title = typedArray.getString(R.styleable.CommonRowEntranceView_row_txt);
        //使用
        if (bgDrawable == null) {
            mRootLayout.setBackgroundColor(Color.WHITE);
        } else {
            mRootLayout.setBackground(bgDrawable);
        }
        if (!TextUtils.isEmpty(title)) {
            mTitle.setText(title);
        }
        mTitle.setTextColor(txtColor);
        //释放资源
        typedArray.recycle();
    }
}
